package com.hxwm.hwdbtool.entity.dbmigration;

import com.hxwm.hwdbtool.util.HwUtil;

import java.util.ArrayList;
import java.util.List;

public class MapperTable {
    private String dbName;//库名
    private String userName;//用户名
    private String tableName;//表名
    private String dataSourceKeyName;//数据源唯一名称
    private List<MapperField> tableFields;//行

    public MapperTable(String dbTableName) {
        if (!HwUtil.isNullOrEmpty(dbTableName)){
            String[] nameArray = dbTableName.split("\\.");
            if(nameArray.length == 1){
                this.tableName = dbTableName;
            }
            if(nameArray.length == 2){
                this.dbName = nameArray[0];
                this.tableName = nameArray[1];
            }
            if(nameArray.length == 3){
                this.dbName = nameArray[0];
                this.userName = nameArray[1];
                this.tableName = nameArray[2];
                this.dataSourceKeyName = dbName + "-" + userName;
            }
        }
        this.tableFields = new ArrayList<>();
    }

    /**
     * 返回全字段sql
     * @return
     */
    @Override
    public String toString(){
        String fieldNames="";
        if(tableFields!=null) {
            for (MapperField tabFields : tableFields) {
                //过滤无效空行
                if(tabFields.getFieldName()!=null && !"".equals(tabFields.getFieldName())) {
                    fieldNames = fieldNames + tabFields.getFieldName() + ",";
                }
            }
            fieldNames = fieldNames.substring(0, fieldNames.lastIndexOf(","));
        }
        return fieldNames;
    }

    public String getDbName() {
        return dbName;
    }

    public void setDbName(String dbName) {
        this.dbName = dbName;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getDataSourceKeyName() {
        return dataSourceKeyName;
    }

    public void setDataSourceKeyName(String dataSourceKeyName) {
        this.dataSourceKeyName = dataSourceKeyName;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }
    public List<MapperField> getTableFields() {
        return tableFields;
    }

    public void setTableFields(List<MapperField> tableFields) {
        this.tableFields = tableFields;
    }
}
